﻿using mscook.core.domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Linq;

namespace mscook.core.repositories
{
    public class ResourceTypeRepository : Repository<ResourceType>, mscook.core.repositories.IResourceTypeRepository
    {

        public IList<ResourceType> GetParentResourceTypeByLimit(string category, int start, int limit, out long total)
        {
            var query = (from x in this.Session.Query<ResourceType>()
                         where x.IsDelete == false
                 && (string.IsNullOrEmpty(category) || x.ResourceCategory.ToString() == category)
                 && x.ParentType == null

                         select x).OrderBy(o => o.ResourceCategory);
            total = query.Count();
            return query.Skip(start).Take(limit).ToList();


        }

        public IList<ResourceType> GetParentResourceTypeByLimit(string category, int start, int limit, out long total, System.Linq.Expressions.Expression<Func<ResourceType, bool>> expression)
        {
            var query = (from x in this.Session.Query<ResourceType>()
                         where x.IsDelete == false
                 && (string.IsNullOrEmpty(category) || x.ResourceCategory.ToString() == category)
                 && x.ParentType == null

                         select x).Where(expression).OrderBy(o => o.ResourceCategory);
            total = query.Count();
            return query.Skip(start).Take(limit).ToList();


        }

        public IList<ResourceType> GetChildTypesByLimit(string category, int start, int limit, out long total)
        {
            var query = (from x in this.Session.Query<ResourceType>()
                         where x.IsDelete == false
                 && (string.IsNullOrEmpty(category) || x.ResourceCategory.ToString() == category)
                 && x.ParentType != null
                 && x.ParentType.IsDelete == false
                 && x.IsDelete == false
                         select x).OrderBy(o => o.ResourceCategory).OrderBy(o => o.ParentType).Distinct();
            total = query.Count();
            return query.Skip(start).Take(limit).ToList();
        }

        public IList<ResourceType> GetHotChildTypesByLimit(string category, int start, int limit, out long total)
        {
            var query = (from x in this.Session.Query<ResourceOfType>()
                         where x.IsDelete == false
                 && (string.IsNullOrEmpty(category) || x.ResourceType.ResourceCategory.ToString() == category)
                 && x.ResourceType.ParentType != null
                 && x.ResourceType.ParentType.IsDelete == false
                 &&x.IsDelete==false
                         select x.ResourceType).OrderBy(o => o.ResourceCategory).OrderBy(o => o.ParentType).Distinct();
            total = query.Count();
            return query.Skip(start).Take(limit).ToList();
        }


        public IList<ResourceType> GetAllParentResourceType(string category)
        {
            var query = (from x in this.Session.Query<ResourceType>()
                         where x.IsDelete == false
                 && (string.IsNullOrEmpty(category) || x.ResourceCategory.ToString() == category)
                 && x.ParentType == null

                         select x);
            return query.ToList();


        }
        public IList<ResourceType> GetAllChildResourceType(string category)
        {
            var query = (from x in this.Session.Query<ResourceOfType>()
                         where x.IsDelete == false
                 && (string.IsNullOrEmpty(category) || x.ResourceType.ResourceCategory.ToString() == category)
                 && x.ResourceType.ParentType != null
                 && x.ResourceType.ParentType.IsDelete == false

                         select x.ResourceType).Distinct();
            return query.ToList();


        }

        public IList<ResourceType> GetResourceByParent(string parentId)
        {
            var query = (from x in this.Session.Query<ResourceType>()
                         where x.IsDelete == false
                 && x.ParentType.Id == parentId
                   && x.ParentType.IsDelete == false
                         select x);

            return query.ToList();
        }



        public IList<string> GetExitTypeIds(string resourceId)
        {
            var query = (from x in this.Session.Query<ResourceOfType>()
                         where x.IsDelete == false
                 && x.ResourceId == resourceId
                         select x.ResourceType.Id).Distinct();
            return query.ToList();
        }

        public IList<string> GetResourceByType(PurchasedCategory category, string typeId)
        {
            var query = (from x in this.Session.Query<ResourceOfType>()
                         where x.IsDelete == false
                 && x.ResourceType.ResourceCategory == category
                 && x.ResourceType.Id == typeId

                         select x.ResourceId).Distinct();

            return query.ToList();
        }

        public IList<string> GetResourceByTypes(PurchasedCategory category, IList<string> typeId)
        {
            var query = (from x in this.Session.Query<ResourceOfType>()
                         where x.IsDelete == false
                 && x.ResourceType.ResourceCategory == category
                 && typeId.Contains(x.ResourceType.Id)

                         select x.ResourceId).Distinct();

            return query.ToList();
        }




        public IList<ResourceType> GetTypeByResource(PurchasedCategory category, string resourceId)
        {
            var query = (from x in this.Session.Query<ResourceOfType>()
                         where x.IsDelete == false
                 && (category == null || x.ResourceType.ResourceCategory == category)
                 && x.ResourceId == resourceId
                         select x.ResourceType).Distinct();
            return query.ToList();
        }

        public IList<ResourceType> GetTypeByResource(string resourceId)
        {
            var query = (from x in this.Session.Query<ResourceOfType>()
                         where x.IsDelete == false

                 && x.ResourceId == resourceId
                         select x.ResourceType).Distinct();
            return query.ToList();
        }

        public IList<ResourceType> GetTypeByCategory(PurchasedCategory category)
        {
            var query = (from x in this.Session.Query<ResourceType>()
                         where x.IsDelete == false

                 && x.ResourceCategory == category
                         select x).Distinct();
            return query.ToList();
        }

    }
}
